import http from '../http'
const httpx = (url, type, params, exConfig, exData) => {
	switch (type.toUpperCase()) {
		case 'GET':
			return http.get(`/kd` + url, params, exConfig, exData);
			break;
		case 'POST':
			return http.post(`/kd` + url, params, exConfig, exData);
			break;
		case 'PUT':
			return http.put(`/kd` + url, params, exConfig, exData);
			break;
		case 'DELETE':
			return http.delete(`/kd` + url, params);
			break;
	}
}

// 每次调用都先登录
const beforeLogin = (isAdmLogin) => {
	let sInfo = uni.getStorageSync('account')
	if (isAdmLogin) {
		let config = uni.getStorageSync('config')
		let obj = {
			username: config.admAccount[0],
			password: config.admAccount[1]
		}
		Object.assign(sInfo, obj)
	}
	return login(sInfo)
}

// 登录验证
const login = data => httpx('/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc', 'POST', data)
// 查看表单数据
const formView = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc', 'POST', data)
}
// 保存表单数据
const formSave = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc', 'POST', data)
}
// 暂存表单数据
const formDraft = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Draft.common.kdsvc', 'POST', data)
}
// 提交表单数据
const formSubmit = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc', 'POST', data)
}
// 审核表单数据
const formAudit = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc', 'POST', data)
}
// 反审核表单数据
const formUnAudit = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc', 'POST', data)
}
// 删除表单数据
const formDelete = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc', 'POST', data)
}
// 表单数据查询
const formQuery = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc', 'POST', data, ['arrayToObject'])
}

// 附件上传
const fileUpLoad = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.AttachmentUpLoad.common.kdsvc', 'POST', data)
}

// 附件下载
const fileDownLoad = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.AttachmentDownLoad.common.kdsvc', 'POST', data)
}

// 修改密码
const changePassword = async (data, noBeforeLogin, isAdmLogin) => {
	if (!noBeforeLogin) await beforeLogin(isAdmLogin)
	return httpx('/Kingdee.BOS.WebApi.ServicesStub.UserService.ResetUserPassWord.common.kdsvc', 'POST', data)
}

export default {
	login,
	formView,
	formSave,
	formDraft,
	formSubmit,
	formAudit,
	formUnAudit,
	formDelete,
	formQuery,
	fileUpLoad,
	fileDownLoad,
	changePassword
}